package com.example.qiehuanactivity;

import java.util.List;

import com.example.android.navigationdrawerexample.R;


import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Bitmap.Config;
import android.graphics.Paint.Cap;
import android.os.Handler;
import android.os.Message;
import android.view.View;

public class RedBackBar2 extends View {


	Bitmap mBitQQ = null;

	Paint mPaint = null;


	Bitmap mSCBitmap = null;


	Canvas mCanvas = null;
	float scale = 0;
	List<Integer> listRandom = null;
	int X[] = new int[27];
	MyHandler myHandler;
	int indx = -400;
	int width, hight;
	int locate_y;
	int maxind;
	List<String> y_Name = null;
	List<Integer> numberDwon = null;
	public RedBackBar2(Context context, int w, int h, int locate_y,
			List<Integer> listRandom, List<String> y_Name,
			List<Integer> numberDown) {
		super(context);
		this.listRandom = listRandom;
		width = w;
		hight = h;
		this.locate_y = locate_y;
		this.y_Name = y_Name;
		this.numberDwon = numberDown;
		scale = context.getResources().getDisplayMetrics().density;
		mSCBitmap = Bitmap.createBitmap(w, dip2px(170), Config.ARGB_8888);
		
		for (int i = 0; i < 27; i++) {
			X[i] = i * 10 + 42;
		}
		DrawBackgroud(w, h);
		myHandler = new MyHandler();
		new TimeThread2().start();

	}

	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		setMeasuredDimension(dip2px(400), dip2px(170));
	}

	public int dip2px(float dpValue) {

		return (int) (dpValue * scale + 0.5f);
	}

	void drawText(Canvas canvas, String text, float x, float y, Paint paint,
			float angle) {
		if (angle != 0) {
			canvas.rotate(angle, x, y);
		}
		canvas.drawText(text, x, y, paint);
		if (angle != 0) {
			canvas.rotate(-angle, x, y);
		}
	}

	public void onDraw(Canvas canvas) {
		super.onDraw(canvas);

		canvas.drawBitmap(mSCBitmap, 0, 0, mPaint);
	}

	void DrawBackgroud(int w, int h) {
		mCanvas = new Canvas();
		mCanvas.setBitmap(mSCBitmap);
		mPaint = new Paint();
		mPaint.setStrokeCap(Cap.ROUND);
		mPaint.setAntiAlias(true);
	}

	private class MyHandler extends Handler {
		public void handleMessage(Message msg) {
			
			if (msg.what < 20) {
				mPaint.setColor(Color.rgb(255, 128, 0));
				mCanvas.drawRect(dip2px(41), dip2px(63), dip2px(315),
						dip2px(96), mPaint);
				// ---------
				mPaint.setColor(Color.WHITE);
				for (int i = 0; i < 27; i++) {
					if (msg.arg1 > listRandom.get(i)) {
						mCanvas.drawRect(dip2px(X[i]),
								dip2px(130 - listRandom.get(i)),
								dip2px(X[i] + 8), dip2px(130 + locate_y),
								mPaint);
					} else {
						mCanvas.drawRect(dip2px(X[i]), dip2px(130 - msg.arg1),
								dip2px(X[i] + 8), dip2px(130 + locate_y),
								mPaint);
					}
				}

			} else if (msg.what == 22) {
				DrawBackGround(0);
			}else if(msg.what == 23) {
				mPaint.setColor(Color.WHITE);
				mPaint.setStrokeWidth(dip2px(2));
				if(msg.arg1 <= 100) {
					mCanvas.drawLine(dip2px(40), dip2px(130), dip2px(40), dip2px(130 - msg.arg1), mPaint);
				}
				mCanvas.drawLine(dip2px(40), dip2px(130), dip2px(40 + msg.arg1), dip2px(130), mPaint);
			} else if (msg.what == 24) {
				mPaint.setColor(Color.rgb(255, 128, 0));
				mCanvas.drawRect(dip2px(41), dip2px(63), dip2px(msg.arg1),
						dip2px(96), mPaint);
			} else if(msg.what == 25) {
				mPaint.setTextSize(dip2px(14));
				drawText(mCanvas,"Step frequency", dip2px(35), dip2px(124), mPaint, 270);
				mCanvas.drawText(y_Name.get(6), dip2px(315),
						dip2px(142 + locate_y), mPaint);
			}
			postInvalidate();
		}
	}

	class TimeThread2 extends Thread {
		public void run() {
			try {
				sleep(500);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			for (int i = 0; i <= 260; i+=2) {
				Message sendMsg = myHandler.obtainMessage();
				sendMsg.what = 23;
				sendMsg.arg1 = i;
				myHandler.sendMessage(sendMsg);
				try {
					sleep(10);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			Message sendMsg2 = myHandler.obtainMessage();
			sendMsg2.what = 25;
			myHandler.sendMessage(sendMsg2);
			for (int i = 41; i <= 316; i += 5) {
				Message sendMsg = myHandler.obtainMessage();
				sendMsg.what = 24;
				if (i >= 315) {
					i = 315;
				}
				sendMsg.arg1 = i;
				myHandler.sendMessage(sendMsg);
				try {
					sleep(5);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			Message sendMsg = myHandler.obtainMessage();
			sendMsg.what = 22;
			myHandler.sendMessage(sendMsg);
			new TimeThread().start();
		}
	}

	// 画图
	class TimeThread extends Thread {

		@Override
		public void run() {
			try {
				sleep(500);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			maxind = 0;
			for (int i = 1; i < 21; i++) {
				if (listRandom.get(i) > listRandom.get(maxind)) {
					maxind = i;
				}
			}
			for (int j = 0; j < listRandom.get(maxind); j += 8) {
				Message sendMsg = myHandler.obtainMessage();
				sendMsg.what = 10;
				sendMsg.arg1 = j;
				myHandler.sendMessage(sendMsg);
				try {
					sleep(60);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
		}
	}

	void DrawBackGround(int arg1) {
		mPaint.setAntiAlias(true);
		mPaint.setColor(Color.WHITE);
		mPaint.setStrokeWidth(dip2px(2));
		mCanvas.drawLine(dip2px(40), dip2px(130 + locate_y),
				dip2px(315), dip2px(130 + locate_y), mPaint);
		mPaint.setStrokeWidth(dip2px(2));
		mCanvas.drawLine(dip2px(40), dip2px(130 + locate_y),
				dip2px(40), dip2px(30 + locate_y), mPaint);
		mPaint.setTextSize(dip2px(14));
		drawText(mCanvas,"Step frequency", dip2px(35), dip2px(124), mPaint, 270);
		mCanvas.drawText(y_Name.get(6), dip2px(315),
				dip2px(142 + locate_y), mPaint);
		mPaint.setColor(Color.WHITE);
		mPaint.setTextSize(dip2px(12));
		mCanvas.drawText(y_Name.get(0) + "\n1~2", dip2px(arg1 + 315),
				dip2px(45 + locate_y), mPaint);
		mCanvas.drawText(y_Name.get(1), dip2px(arg1 + 315),
				dip2px(56 + locate_y), mPaint);
		mCanvas.drawText(y_Name.get(2), dip2px(arg1 + 315),
				dip2px(77 + locate_y), mPaint);
		mCanvas.drawText(y_Name.get(3), dip2px(arg1 + 315),
				dip2px(88 + locate_y), mPaint);
		mCanvas.drawText(y_Name.get(4), dip2px(arg1 + 315),
				dip2px(110 + locate_y), mPaint);
	}
}